//! 人脸图片上传模块
mod face_data;
use super::stdin;
use super::http_client::client;
use super::file;
use super::util::base64_utils;
/**
 * 读取ip地址
 */
fn read_ip() -> String {
    println!("请输入摄像机ip地址:");
    stdin::read_in::read_line()
    
}

/**
 * 读取 人脸图片所在文件夹
 */
fn read_face_dir() -> String {
    println!("请输入人脸图片所在文件夹路径:");
    stdin::read_in::read_line()
}

/**
 * 上传人脸图片
 */
pub fn upload_face() {
    let ip = read_ip();
    let upload_api = format!("{}{}{}", "http://", ip.trim(), "/person/add2");

    //C:\Users\mdjro\Desktop\faces
    let face_dir = read_face_dir();
    //读取文件夹下的图片文件
    let images = file::read_image_path(face_dir.trim());
    println!("已读取的图片文件:");
    for path in &images {
        println!("{}", path);
    }
    println!();
    for path in &images {
        let picture = base64_utils::encode_file(path).unwrap();
        let start = path.rfind("\\").unwrap();
        let end = path.find(".").unwrap_or(path.len());
        let face_id = String::from(&path[start+1 .. end ]);
        println!("face_id:{}", &face_id);
        let data = face_data::FaceData::new(face_id, picture);
        let json = json::stringify(data);
        let r = client::block_post_json(upload_api.as_str(), &json).expect("上传失败");
        println!("{}上传结果:{}", path, r);
        println!();
    }
 

}
